<?php

namespace app\job;

use app\model\GoodsOrders;
use Exception;
use think\db\exception\DataNotFoundException;
use think\queue\Job;

class Task
{
	
	
	
	public function fire(Job $job, $data)
	{
		
		$rt = $this->doJob($data);
		if ($rt) {
			$job->delete();
			return true;
		}
		
		// 重试三次失败
			if ($job->attempts() >= 3) {
				$job->release(60*60*24);
				return false;
			}
		
		//执行失败10S后重试
		$job->release(10);
		
	}
	
	public function doJob($data)
	{
		try {
			if (array_key_exists('action', $data)) {
				//超时系统自动操作
				switch ($data['action']) {
					case 'goods_orders_timeout':
						//订单超时取消
						$order_id = $data['orders_id'];
						$row = GoodsOrders::findOrFail($order_id);
						if ($row->status == 0) {
							$row->status = 5;
							$row->save();
						}
						break;
				}
			}
		} catch (DataNotFoundException $e) {
			return true;
		} catch (Exception $e) {
			return false;
		}
		return true;
	}
	
	
}
